﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace INTROSE
{
    public partial class Edit_Transaction : Form
    {
        SqlConnection con = new SqlConnection("Data Source = .\\SQLEXPRESS; Database = Introse; Integrated Security = True");
        private string name, position;
        private int num;
        public Edit_Transaction(string name, string position, int a)
        {
            InitializeComponent();
            this.name = name;
            this.position = position;
            this.num = a;
            con.Open();
            SqlCommand command = new SqlCommand("select * from orders where transNo = '" + num + "'", con);
            SqlDataReader reader = command.ExecuteReader();
            if (reader.HasRows)
            {
                reader.Read();
                lblTransNumGen.Text = reader.GetInt32(0).ToString();
                txtDateOrdered.Text = reader.GetDateTime(1).ToString();
                txtName.Text = reader.GetString(2);
                txtDatePaid.Text = reader.GetDateTime(4).ToString();
                txtAddress.Text = reader.GetString(3);
                txtDateClaimed.Text = reader.GetDateTime(7).ToString();
                if (reader.GetBoolean(5) == false)
                    radioButtonUnpaid.Checked = true;
                else
                    radioButtonPaid.Select();
                if (reader.GetBoolean(9) == false)
                {
                    radioButtonPickUp.Checked = true;
                    if (reader.GetBoolean(8) == false)
                        radioButtonUnclaimed.Checked = true;
                    else
                    {
                        radioButtonClaimed.Checked = true;
                    }
                }
                else
                {
                    radioButtonDelivery.Checked = true;
                    if (reader.GetBoolean(6) == false)
                        radioButtonUnclaimed.Checked = true;
                    else
                    {
                        radioButtonClaimed.Checked = true;
                    }
                }
            }
            reader.Close();
            SqlCommand command1 = new SqlCommand("select * from orderItems where transNo = '" + num + "'", con);
            SqlDataReader reader1 = command1.ExecuteReader();
            if (reader1.HasRows)
            {
                reader1.Read();
                txt100cc.Text = reader1.GetInt32(1).ToString();
                txtp100cc.Text = reader1.GetInt32(6).ToString();
                txtTotal.Text = reader1.GetInt32(11).ToString();
                if (txt100cc.Text != "0")
                    chckBox100cc.Checked = true;
                else
                {
                    txt100cc.Enabled = false;
                    txtp100cc.Enabled = false;
                }
                txt200cc.Text = reader1.GetInt32(2).ToString();
                txtp200cc.Text = reader1.GetInt32(7).ToString();
                if (txt200cc.Text != "0")
                    chckBox200cc.Checked = true;
                else
                {
                    txt200cc.Enabled = false;
                    txtp200cc.Enabled = false;
                }
                txt250cc.Text = reader1.GetInt32(3).ToString();
                txtp250cc.Text = reader1.GetInt32(8).ToString();
                if (txt250cc.Text != "0")
                    chckBox250cc.Checked = true;
                else
                {
                    txt250cc.Enabled = false;
                    txtp250cc.Enabled = false;
                }
                txt400cc.Text = reader1.GetInt32(4).ToString();
                txtp400cc.Text = reader1.GetInt32(9).ToString();
                if (txt400cc.Text != "0")
                    chckBox400cc.Checked = true;
                else
                {
                    txt400cc.Enabled = false;
                    txtp400cc.Enabled = false;
                }
                txt500cc.Text = reader1.GetInt32(5).ToString();
                txtp500cc.Text = reader1.GetInt32(10).ToString();
                if (txt500cc.Text != "0")
                    chckBox500cc.Checked = true;
                else
                {
                    txt500cc.Enabled = false;
                    txtp500cc.Enabled = false;
                }
            }
            reader1.Close();
            SqlCommand command2 = new SqlCommand("SELECT * FROM sizes WHERE size = '100cc'", con);
            SqlDataReader reader2 = command2.ExecuteReader();
            if (reader2.HasRows)
            {
                reader2.Read();
                txtp100cc.Text = reader2.GetInt32(1).ToString();
            }
            reader2.Close();
            command2 = new SqlCommand("SELECT * FROM sizes WHERE size = '200cc'", con);
            reader2 = command2.ExecuteReader();
            if (reader2.HasRows)
            {
                reader2.Read();
                txtp200cc.Text = reader2.GetInt32(1).ToString();
            }
            reader2.Close();
            command2 = new SqlCommand("SELECT * FROM sizes WHERE size = '250cc'", con);
            reader2 = command2.ExecuteReader();
            if (reader2.HasRows)
            {
                reader2.Read();
                txtp250cc.Text = reader2.GetInt32(1).ToString();
            }
            reader2.Close();
            command2 = new SqlCommand("SELECT * FROM sizes WHERE size = '400cc'", con);
            reader2 = command2.ExecuteReader();
            if (reader2.HasRows)
            {
                reader2.Read();
                txtp400cc.Text = reader2.GetInt32(1).ToString();
            }
            reader2.Close();
            command2 = new SqlCommand("SELECT * FROM sizes WHERE size = '500cc'", con);
            reader2 = command2.ExecuteReader();
            if (reader2.HasRows)
            {
                reader2.Read();
                txtp500cc.Text = reader2.GetInt32(1).ToString();
            }
            con.Close();
            reader2.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
            Edit_Transactions menu = new Edit_Transactions(name, position);
            menu.Show();
        }

        private void Edit_Transaction_Load(object sender, EventArgs e)
        {
            btnSave.Enabled = false;
        }

        private void btnCompute_Click(object sender, EventArgs e)
        {
                int txt100 = 0, txt200 = 0, txt250 = 0, txt400 = 0, txt500 = 0, total = 0, checker1 = 0, checker2 = 0, checker3 = 0, checker4 = 0, checker5 = 0;
            if (txt100cc.Text == "" && txt200cc.Text == "" && txt250cc.Text == "" && txt400cc.Text == "" && txt500cc.Text == "")
                MessageBox.Show("No orders were made! Please order atleast one");
            if (chckBox100cc.Checked == true || chckBox200cc.Checked == true || chckBox250cc.Checked == true || chckBox400cc.Checked == true || chckBox500cc.Checked == true)
            {
                if (txt100cc.Text == "")
                    txt100cc.Text = "0";
                else if (txt100cc.Text[0] < 48 || txt100cc.Text[0] >= 57)
                {
                    checker1 = 1;
                    MessageBox.Show("Please enter numbers only");
                }
                else
                {
                    checker1 = 0;
                    txt100 = Int32.Parse(txt100cc.Text);
                }
                if (txt200cc.Text == "")
                    txt200cc.Text = "0";
                else if (txt200cc.Text[0] < 48 || txt200cc.Text[0] >= 57)
                {
                    checker2 = 1;
                    MessageBox.Show("Please enter numbers only");
                }
                else
                {
                    checker2 = 0;
                    txt200 = Int32.Parse(txt200cc.Text);
                }
                if (txt250cc.Text == "")
                    txt250cc.Text = "0";
                else if (txt250cc.Text[0] < 48 || txt250cc.Text[0] >= 57)
                {
                    checker3 = 1;
                    MessageBox.Show("Please enter numbers only");
                }
                else
                {
                    checker3 = 0;
                    txt250 = Int32.Parse(txt250cc.Text);
                }
                if (txt400cc.Text == "")
                    txt400cc.Text = "0";
                else if (txt400cc.Text[0] < 48 || txt400cc.Text[0] >= 57)
                {
                    checker4 = 1;
                    MessageBox.Show("Please enter numbers only");
                }
                else
                {
                    checker4 = 0;
                    txt400 = Int32.Parse(txt400cc.Text);
                }
                if (txt500cc.Text == "")
                    txt500cc.Text = "0";
                else if (txt500cc.Text[0] < 48 || txt500cc.Text[0] >= 57)
                {
                    checker5 = 1;
                    MessageBox.Show("Please enter numbers only");
                }
                else
                {
                    checker5 = 0;
                    txt500 = Int32.Parse(txt500cc.Text);
                }
            }
            else
                MessageBox.Show("Please check atleast one size of the product.");
            if (checker1 == 0 && checker2 == 0 && checker3 == 0 && checker4 == 0 && checker5 == 0)
            {
                total = (txt100 * Int32.Parse(txt100cc.Text)) + (txt200 * Int32.Parse(txt200cc.Text)) + (txt250 * Int32.Parse(txt250cc.Text)) + (txt400 * Int32.Parse(txt400cc.Text)) + (txt500 * Int32.Parse(txt500cc.Text));
                txtTotal.Text = total.ToString();
                btnSave.Enabled = true;
            }
            else
                txtTotal.Text = "0";
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            con.Open();
            SqlCommand command = new SqlCommand("UPDATE orders SET custName = '" + txtName.Text + "' custAdd = '" + txtAddress.Text + "'", con);
            if (radioButtonDelivery.Checked)
            {
                command = new SqlCommand("UPDATE orders SET modDistr = '1' WHERE transNo = '" + num + "'", con);
                command.ExecuteNonQuery();
                if (radioButtonPaid.Checked)
                {
                    command = new SqlCommand("UPDATE orders SET paid = '1', datePay = '" + System.DateTime.Now + "' WHERE transNo = '" + num + "'", con);
                    command.ExecuteNonQuery();
                }
                else if (radioButtonUnpaid.Checked)
                {
                    command = new SqlCommand("UPDATE orders SET paid = '0' WHERE transNo = '" + num + "'", con);
                    command.ExecuteNonQuery();
                }
                if (radioButtonClaimed.Checked)
                {
                    command = new SqlCommand("UPDATE orders SET delivered = '1', claimed = '" + null + "' WHERE transNo = '" + num + "'", con);
                    command.ExecuteNonQuery();
                }
                else if (radioButtonUnclaimed.Checked)
                {
                    command = new SqlCommand("UPDATE orders SET delivered = '0', claimed = '" + null + "' WHERE transNo = '" + num + "'", con);
                    command.ExecuteNonQuery();
                }
            }
            else if (radioButtonPickUp.Checked)
            {
                command = new SqlCommand("UPDATE orders SET modDistr = '0' WHERE transNo = '" + num + "'", con);
                command.ExecuteNonQuery();
                if (radioButtonPaid.Checked)
                {
                    command = new SqlCommand("UPDATE orders SET paid = '1' WHERE transNo = '" + num + "'", con);
                    command.ExecuteNonQuery();
                }
                else if (radioButtonUnpaid.Checked)
                {
                    command = new SqlCommand("UPDATE orders SET paid = '0' WHERE transNo = '" + num + "'", con);
                    command.ExecuteNonQuery();
                }
                if (radioButtonClaimed.Checked)
                {
                    command = new SqlCommand("UPDATE orders SET delivered = '" + null + "', claimed = '1', dateClaim = '" + System.DateTime.Now + "' WHERE transNo = '" + num + "'", con);
                    command.ExecuteNonQuery();
                }
                else if (radioButtonUnclaimed.Checked)
                {
                    command = new SqlCommand("UPDATE orders SET delivered = '" + null + "', claimed = '0' WHERE transNo = '" + num + "'", con);
                    command.ExecuteNonQuery();
                }
            }
            SqlCommand command1 = new SqlCommand("SELECT * FROM orderItems WHERE transNo = '" + num + "'", con);
            SqlDataReader reader1 = command1.ExecuteReader();
            SqlCommand com;
            SqlDataReader rea;
            reader1.Close();
            if (chckBox100cc.Checked)
            {
                com = new SqlCommand("SELECT * FROM orderItems WHERE transNo = '" + num + "' AND size = '100cc'", con);
                rea = com.ExecuteReader();
                if (rea.HasRows)
                {
                    SqlCommand command2 = new SqlCommand("UPDATE orderItems SET quantity = '" + txt100cc.Text + "' where size = '100cc' AND transNo = '" + num + "'", con);
                    command2.ExecuteNonQuery();
                }
                else
                {
                    SqlCommand command2 = new SqlCommand("INSERT INTO orderItems VALUES('" + num + "', '" + txt100cc.Text + "', '100cc')", con);
                    command2.ExecuteNonQuery();
                }
            }
            if (chckBox200cc.Checked)
            {
                com = new SqlCommand("SELECT * FROM orderItems WHERE transNo = '" + num + "' AND size = '200cc'", con);
                rea = com.ExecuteReader();
                if (rea.HasRows)
                {
                    SqlCommand command2 = new SqlCommand("UPDATE orderItems SET quantity = '" + txt200cc.Text + "' where size = '200cc' AND transNo = '" + num + "'", con);
                    command2.ExecuteNonQuery();
                }
                else
                {
                    SqlCommand command2 = new SqlCommand("INSERT INTO orderItems VALUES('" + num + "', '" + txt200cc.Text + "', '200cc')", con);
                    command2.ExecuteNonQuery();
                }
            }
            if (chckBox250cc.Checked)
            {
                com = new SqlCommand("SELECT * FROM orderItems WHERE transNo = '" + num + "' AND size = '250cc'", con);
                rea = com.ExecuteReader();
                if (rea.HasRows)
                {
                    SqlCommand command2 = new SqlCommand("UPDATE orderItems SET quantity = '" + txt250cc.Text + "' where size = '250cc' AND transNo = '" + num + "'", con);
                    command2.ExecuteNonQuery();
                }
                else
                {
                    SqlCommand command2 = new SqlCommand("INSERT INTO orderItems VALUES('" + num + "', '" + txt250cc.Text + "', '250cc')", con);
                    command2.ExecuteNonQuery();
                }
            }
            if (chckBox400cc.Checked)
            {
                com = new SqlCommand("SELECT * FROM orderItems WHERE transNo = '" + num + "' AND size = '400cc'", con);
                rea = com.ExecuteReader();
                if (rea.HasRows)
                {
                    SqlCommand command2 = new SqlCommand("UPDATE orderItems SET quantity = '" + txt400cc.Text + "' where size = '400cc' AND transNo = '" + num + "'", con);
                    command2.ExecuteNonQuery();
                }
                else
                {
                    SqlCommand command2 = new SqlCommand("INSERT INTO orderItems VALUES('" + num + "', '" + txt400cc.Text + "', '400cc')", con);
                    command2.ExecuteNonQuery();
                }
            }
            if (chckBox500cc.Checked)
            {
                com = new SqlCommand("SELECT * FROM orderItems WHERE transNo = '" + num + "' AND size = '500cc'", con);
                rea = com.ExecuteReader();
                if (rea.HasRows)
                {
                    SqlCommand command2 = new SqlCommand("UPDATE orderItems SET quantity = '" + txt500cc.Text + "' where size = '500cc' AND transNo = '" + num + "'", con);
                    command2.ExecuteNonQuery();
                }
                else
                {
                    SqlCommand command2 = new SqlCommand("INSERT INTO orderItems VALUES('" + num + "', '" + txt500cc.Text + "', '500cc')", con);
                    command2.ExecuteNonQuery();
                }
            }
            this.Close();
            Input_Employee_Name menu = new Input_Employee_Name(name, position);
            menu.Show();
        }

        private void chckBox100cc_CheckedChanged(object sender, EventArgs e)
        {
            if (chckBox100cc.Checked)
            {
                txt100cc.Enabled = true;
                txtp100cc.Enabled = true;
            }
            else
            {
                txt100cc.Enabled = false;
                txtp100cc.Enabled = false;
                txt100cc.Text = "0";
                txtp100cc.Text = "0";
            }
        }

        private void chckBox200cc_CheckedChanged(object sender, EventArgs e)
        {
            if (chckBox200cc.Checked)
            {
                txt200cc.Enabled = true;
                txtp200cc.Enabled = true;
            }
            else
            {
                txt200cc.Enabled = false;
                txtp200cc.Enabled = false;
                txt200cc.Text = "0";
                txtp200cc.Text = "0";
            }
        }

        private void chckBox250cc_CheckedChanged(object sender, EventArgs e)
        {
            if (chckBox250cc.Checked)
            {
                txt250cc.Enabled = true;
                txtp250cc.Enabled = true;
            }
            else
            {
                txt250cc.Enabled = false;
                txtp250cc.Enabled = false;
                txt250cc.Text = "0";
                txtp250cc.Text = "0";
            }
        }

        private void chckBox400cc_CheckedChanged(object sender, EventArgs e)
        {
            if (chckBox200cc.Checked)
            {
                txt400cc.Enabled = true;
                txtp400cc.Enabled = true;
            }
            else
            {
                txt400cc.Enabled = false;
                txtp400cc.Enabled = false;
                txt400cc.Text = "0";
                txtp400cc.Text = "0";
            }
        }

        private void chckBox500cc_CheckedChanged(object sender, EventArgs e)
        {
            if (chckBox200cc.Checked)
            {
                txt500cc.Enabled = true;
                txtp500cc.Enabled = true;
            }
            else
            {
                txt500cc.Enabled = false;
                txtp500cc.Enabled = false;
                txt500cc.Text = "0";
                txtp500cc.Text = "0";
            }
        }
    }
}
